<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="机台" prop="machineId">
        <el-select filterable v-model="queryParams.machineId">
          <el-option
            v-for="item in workbranchList"
            :label="item.machineName"
            :key="item.pid"
            :value="item.pid"
          ></el-option>
        </el-select>
        <!-- <el-input
            v-model.trim="queryParams.machineId"
            style="width: 200px"
            placeholder="请输入机台"
            @keyup.enter="handleQuery"
          /> -->
      </el-form-item>
      <el-form-item label="出版编码" prop="pubNo">
        <el-input
          v-model.trim="queryParams.pubNo"
          style="width: 200px"
          placeholder="请输入出版编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产单编码" prop="produceNo">
        <el-input
          v-model.trim="queryParams.produceNo"
          style="width: 200px"
          placeholder="请输入生产单编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button type="primary" plain @click="handlePointsBind"
        >分配装订</el-button
      >
      <!-- <right-toolbar
              v-model:showSearch="showSearch"
              @queryTable="getList"
              :columns="columns"
          ></right-toolbar> -->
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        :data="dataList"
        row-key="productId"
        @expand-change="handleHeaderClick"
      >
        <el-table-column type="expand">
          <template #default="props">
            <div>
              <!-- <p m="t-0 b-2">State: {{ props.row.state }}</p>
              <p m="t-0 b-2">City: {{ props.row.city }}</p>
              <p m="t-0 b-2">Address: {{ props.row.address }}</p>
              <p m="t-0 b-2">Zip: {{ props.row.zip }}</p>
              <h3>Family</h3> -->
              <el-table :data="props.row.bindingMode" border>
                <!-- <el-table-column
                  width="60"
                  label="序号"
                  type="index"
                  align="center"
                /> -->
                <el-table-column
                  align="center"
                  label="生产单编号"
                  prop="produceNo"
                />
                <!-- <el-table-column
                  align="center"
                  label="车间"
                  prop="workshopName"
                /> -->
                <el-table-column
                  align="center"
                  label="交货日期"
                  prop="deliverDate"
                >
                  <template v-slot="{ row }">
                    {{ row.deliverDate && row.deliverDate.split("T")[0] }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="品种" prop="breedType" />
                <el-table-column
                  align="center"
                  label="组成"
                  prop="composition"
                />
                <el-table-column align="center" label="印张" prop="sheet" />
                <el-table-column
                  align="center"
                  label="版材尺寸"
                  prop="sheetSize"
                >
                  <template #default="scope">
                    <dict-tag
                      :options="ems_machine_info_size"
                      :value="scope.row.sheetSize"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  label="订单数量"
                  prop="produceCount"
                />
                <el-table-column
                  align="center"
                  label="装订方式"
                  prop="bindingMode"
                >
                  <template #default="scope">
                    <dict-tag
                      :options="ems_order_binding_mode"
                      :value="scope.row.bindingMode"
                    />
                  </template>
                </el-table-column>
                <el-table-column align="center" label="印刷状态" prop="ysDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.ysDate == '已完成' ||
                        scope.row.ysDate == '印刷中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleViewBindTask(scope.row, '1')"
                        text
                      >
                        {{ scope.row.ysDate }}
                      </el-button>
                    </span>
                    <span
                      style="color: red"
                      v-else-if="scope.row.ysDate == '--'"
                    >
                      {{ scope.row.ysDate }}
                    </span>
                    <span v-else>
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '2')"
                        text
                      >
                        {{ scope.row.ysDate }}
                      </el-button>
                    </span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="覆膜状态" prop="fmDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.fmDate == '已完成' ||
                        scope.row.ysDate == '印刷中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleViewBindTask(scope.row, '4')"
                        text
                      >
                        {{ scope.row.fmDate }}
                      </el-button>
                    </span>
                    <span
                      style="color: red"
                      v-else-if="scope.row.fmDate == '--'"
                    >
                      {{ scope.row.fmDate }}</span
                    >
                    <span v-else>
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '7')"
                        text
                      >
                        {{ scope.row.fmDate }}
                      </el-button>
                    </span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="裁切状态" prop="cqDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.cqDate == '已完成' ||
                        scope.row.ysDate == '印刷中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleViewBindTask(scope.row, '3')"
                        text
                      >
                        {{ scope.row.cqDate }}
                      </el-button>
                    </span>
                    <span
                      style="color: red"
                      v-else-if="scope.row.cqDate == '--'"
                    >
                      {{ scope.row.cqDate }}</span
                    >
                    <span v-else>
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '5,6')"
                        text
                      >
                        {{ scope.row.cqDate }}
                      </el-button>
                    </span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="折页状态" prop="zyDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.zyDate == '已完成' ||
                        scope.row.ysDate == '印刷中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleViewBindTask(scope.row, '2')"
                        text
                      >
                        {{ scope.row.zyDate }}
                      </el-button>
                    </span>
                    <span
                      style="color: red"
                      v-else-if="scope.row.zyDate == '--'"
                    >
                      {{ scope.row.zyDate }}
                    </span>
                    <span v-else>
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '4')"
                        text
                      >
                        {{ scope.row.zyDate }}
                      </el-button>
                    </span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="装订状态" prop="zdDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.zdDate == '已完成' ||
                        scope.row.ysDate == '印刷中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleViewBindTask(scope.row, '5')"
                        text
                      >
                        {{ scope.row.zdDate }}
                      </el-button>
                    </span>
                    <span
                      style="color: red"
                      v-else-if="scope.row.zdDate == '--'"
                    >
                      {{ scope.row.zdDate }}</span
                    >
                    <span v-else>
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '3')"
                        text
                      >
                        {{ scope.row.zdDate }}
                      </el-button>
                    </span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="发货状态" prop="fhDate">
                  <template #default="scope">
                    <el-button
                      type="primary"
                      @click="handleViewCount(scope.row, '3')"
                      text
                    >
                      {{ scope.row.fhDate }}
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="车间"
          width="90"
          align="center"
          prop="workshopName"
          v-if="columns[3].visible"
        />
        <el-table-column
          label="生产单编码"
          min-width="180"
          align="center"
          :show-overflow-tooltip="true"
          prop="produceNo"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleView(row)" text>
              {{ row.produceNo }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="书刊名称"
          align="center"
          min-width="280"
          prop="bookName"
          v-if="columns[2].visible"
        ></el-table-column>
        <el-table-column
          label="出版编码"
          min-width="120"
          align="center"
          prop="pubNo"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="品种"
          min-width="100"
          align="center"
          prop="breedType"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="订单数量"
          min-width="100"
          align="center"
          prop="orderCount"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="加急交货日期"
          width="125"
          align="center"
          prop="urgentDate"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            <span style="color: red">
              {{ parseTime(row.urgentDate) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="分配日期"
          prop="binddate"
          width="120"
        />
        <el-table-column
          label="交货日期"
          min-width="100"
          align="center"
          prop="deliverDate"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            {{ row.deliverDate ? row.deliverDate.split("T")[0] : "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="下单日期"
          min-width="100"
          align="center"
          prop="submitDate"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            {{ row.submitDate ? row.submitDate.split("T")[0] : "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="类型"
          min-width="100"
          align="center"
          prop="type"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            {{ row.type == 1 ? "封面" : "内文" }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="100"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              v-if="scope.row.istake == 1"
              @click="handleSubmit(scope.row)"
            >
              取版
            </el-button>
            <el-tooltip content="打印" placement="top">
              <el-button
                link
                type="primary"
                icon="Printer"
                @click="handlePrint(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-button
              v-hasPermi="['bindtake:temp:submit']"
              link
              type="primary"
              v-if="scope.row.tempStatus != 1"
              @click="handleStopSubmit(scope.row)"
            >
              暂提
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      /> -->
      <printView
        :visible="printOpen"
        :printList="printList"
        @cancel="
          () => {
            printOpen = false;
            printList = [];
          }
        "
      ></printView>
    </div>
  </div>
</template>
  <script setup name="WaitBindTask">
import { downLoadBlob } from "@/utils/ruoyi";
import {
  bindMachineTask,
  bindFMTask,
  tempSubmit,
  fpPrintBinding,
} from "@/api/production/cover";
import {
  getMachine,
  getList as getWorkBenchList,
} from "@/api/basicInfo/workbench";
import printView from "@/components/PrintView";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const workbranchList = ref([]);
import useUserStore from "@/store/modules/user";
const userInfo = useUserStore();
const printOpen = ref(false);
const printList = ref([]);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();
const {
  ems_workshop,
  ems_machine_info_size,
  ems_order_binding_mode,
  ems_machine_info_type,
  ems_machine_info_model,
  ems_order_status,
} = proxy.useDict(
  "ems_workshop",
  "ems_machine_info_type",
  "ems_order_status",
  "ems_machine_info_model",
  "ems_machine_info_size",
  "ems_order_binding_mode"
);
getMachine().then((res) => {
  res.rows.forEach((item) => {
    //item.item.machineId = item.pid;
    item.machineName = item.machineName + "/" + item.workshopName;
  });
  workbranchList.value = res.rows;
  if (res.rows?.length > 0) {
    queryParams.value.machineId = res.rows[0].pid;
  } else {
    getWorkBenchList({
      pageNum: 1,
      pageSize: 100,
      type: "3",
      status: "2",
    }).then((resp) => {
      resp.rows.forEach((item) => {
        //item.machineId = item.pid;
        item.machineName = item.machineName + "/" + item.workshopName;
      });
      workbranchList.value = resp.rows;
    });
  }
  getList();
});

// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `取版编码`, visible: true },
  { key: 2, label: `取版日期`, visible: true },
  { key: 3, label: `取版人`, visible: true },
  { key: 4, label: `备注`, visible: true },
  { key: 5, label: `创建人`, visible: true },
  { key: 6, label: `创建时间`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 50,
  },
});
const { queryParams } = toRefs(data);

const getList = () => {
  loading.value = true;
  dataList.value = [];
  // queryParams.value.machineId = "5bfd2e94fa6d44b3bc4ff2ce4287346b";
  if (queryParams.value.machineId) {
    bindMachineTask(queryParams.value)
      .then((res) => {
        loading.value = false;
        dataList.value = res.rows;
        total.value = res.total;
      })
      .catch((err) => {
        loading.value = false;
      });
  } else {
    loading.value = false;
  }
};

// 查看
const handleView = (row) => {
  if (row.type == 1) {
    //封面
    router.push({
      path: "/production/cover/coverDetails",
      query: {
        id: row.produceId,
        info: true,
        moduleCode: "waitBindTask",
      },
    });
  } else {
    router.push({
      path: "/production/text/textDetails",
      query: {
        id: row.produceId,
        info: true,
        moduleCode: "waitBindTask",
      },
    });
  }
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

const handleHeaderClick = (row, expanded) => {
  bindFMTask({
    productId: row.productId,
    produceId: row.produceId,
    breed: row.breedType,
  }).then((res) => {
    row.bindingMode = res.rows;
  });
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除"' + row.takeCode + '"的单？')
    .then(function () {
      return delTakeVersionInfo(row.id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

// 暂时提交
function handleStopSubmit(row) {
  proxy.$modal
    .confirm("是否确认暂提?")
    .then(function () {
      return tempSubmit({ productId: row.productId });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("暂提成功");
    })
    .catch(() => {});
}
// 提交
const handleSubmit = (row) => {
  router.push({
    path: "/production/waitTakeVersion/waitTakeDetails",
    query: {
      id: row.produceId,
      machineId: queryParams.value.machineId,
      produceNo: row.produceNo,
    },
  });
};

const handlePointsBind = () => {
  if (!queryParams.value.machineId) {
    return proxy.$modal.msgError("请先选择机台");
  }
  loading.value = true;
  fpPrintBinding({ machineId: queryParams.value.machineId })
    .then((res) => {
      loading.value = false;
      getList();
      proxy.$modal.msgSuccess(res.msg);
    })
    .catch((err) => {
      loading.value = false;
    });
};

const handleViewBindTask = (row, type) => {
  router.push({
    path: "/Productionmanagementdistribution/production/searchReportDetail",
    query: {
      breed: row.breedType,
      type: type,
      produceNo: row.produceNo,
      status: -2,
      info: true,
    },
  });
};

// 折页中 印刷中 覆膜中 裁切中
const handleViewLoading = (row, type) => {
  router.push({
    path: "/production/orderProgress/processPenetration",
    query: {
      breedType: row.breedType,
      machineType: type,
      produceNo: row.produceNo,
      info: true,
    },
  });
};

//发货数量
const handleViewCount = (row, type) => {
  router.push({
    path: "/production/orderProgress/quantityPenetration",
    query: {
      produceNo: row.produceNo,
      info: true,
    },
  });
};

const handlePrint = (row) => {
  printList.value = [row];
  printOpen.value = true;
};
// getList();
</script>
  
  <style lang="scss" scoped></style>
  